/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-05-25 16:02:44
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-07-03 14:10:04
 * @FilePath: /tea-garden-web/src/util/storeModules/subject-disease.js
 * @Description: 专题-病虫害管理
 */
import { get } from '@/util/http.js'
import { color } from '@/util/dict.js'
import { DateTime } from 'luxon'
export default {
  state: {
    types: [{ name: '整体', value: '0' }, { name: '茶小绿叶蝉', value: '1' }, { name: '茶炭疽', value: '2' }],
    activeType: '1',
    loading: {
      diseaseStatistics: false,
      diseaseTrend: false,
      diseaseDistribute: false,
      farmlandDisease: false
    },
    diseaseStatistics: null,
    diseaseTrend: [],
    diseaseDistribute: [],
    farmlandDisease: []
  },
  mutations: {
    setActiveType (state, payload) {
      state.activeType = payload
    },
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    // 压力指数统计
    async getDiseaseStatistics ({ state, commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'diseaseStatistics', value: true })
        commit('setCommonState', { key: 'diseaseStatistics', value: null })
        // 该接口提供压力指数数据
        // const { data } = await get({ url: '/Diseases/PressureStatistics', params: { tab_value: state.activeType, massif_id: rootState['base-overview'].currentFarmlandId } })
        const data = {
          degree_status:
          64,
          predict_time:
          '2023-12-11 00:00:00',
          pressure:
          12,
          pressure_diff:
          0
        }
        // const data = { degree_status: 64, predict_time: '2023-12-11 00:00:00', pressure: 12, pressure_diff: 0 }
        commit('setCommonState', { key: 'diseaseStatistics', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'diseaseStatistics', value: false })
    },
    // 压力趋势变化
    async getDiseaseTrend ({ state, commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'diseaseTrend', value: true })
        commit('setCommonState', { key: 'diseaseTrend', value: [] })
        const currentDate = DateTime.now().toFormat('yyyy-MM-dd')
        const pastDate = DateTime.now().minus({ days: 365 }).toFormat('yyyy-MM-dd')
        let { data } = await get({ url: '/Diseases/PressureTrend', params: { tab_value: state.activeType, massif_id: rootState['base-overview'].currentFarmlandId, start_time: pastDate, end_time: currentDate } })
        data = data.reverse().map((item) => {
          // let date = item.predict_time.split('-')
          // date = date[1] + '.' + date[2]
          return { name: item.predict_time, value: item.pressure }
        })
        commit('setCommonState', { key: 'diseaseTrend', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'diseaseTrend', value: false })
    },
    // 压力分布
    async getDiseaseDistribute ({ state, commit, rootGetters }) {
      try {
        commit('setLoadingState', { key: 'diseaseDistribute', value: true })
        commit('setCommonState', { key: 'diseaseDistribute', value: [] })
        const { data } = await get({ url: '/Diseases/PressureDistribute', params: { tab_value: 0 } })
        // const { data } = await get({ url: '/Diseases/PressureDistribute', params: { tab_value: state.activeType } })
        // console.log(data)
        // const data = [{
        //   area_percentage:
        //   1,
        //   degree:
        //   71,
        //   massif_area:
        //   43.12
        // }]
        const area = {}
        const percent = {}
        data.map((item) => {
          area[item.degree] = item.massif_area
          percent[item.degree] = item.area_percentage
          return item
        })
        let parentId = 63
        if (state.activeType === '1' || state.activeType === '2') {
          parentId = 70
        }
        const res = rootGetters['common/getDictByParentId'](parentId).map((item) => {
          item.label = item.name
          item.color = color[item.id]
          item.area = area[item.id] || 0
          item.percent = percent[item.id] || 0
          return item
        })
        // 给下面表单注入数据颜色等
        const diseaseDistribute = [
          { area: 43.12, color: '#1FD1A5', id: 71, label: '健康', name: '健康', parent_id: 70, percent: 1 },
          { area: 0, color: '#BCC8CC', id: 72, label: '轻度', name: '轻度', parent_id: 70, percent: 0 },
          { area: 0, color: '#FFAC54', id: 73, label: '中度', name: '中度', parent_id: 70, percent: 0 },
          { area: 0, color: '#FF6B6B', id: 74, label: '重度', name: '重度', parent_id: 70, percent: 0 }
        ]
        console.log(res)
        // console.log(diseaseDistribute)
        commit('setCommonState', { key: 'diseaseDistribute', value: diseaseDistribute })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'diseaseDistribute', value: false })
    },
    // 地块分布
    async getFarmlandDisease ({ state, commit }) {
      commit('setLoadingState', { key: 'farmlandDisease', value: true })
      try {
        commit('setCommonState', { key: 'farmlandDisease', value: [] })
        // const { data } = await get({ url: '/Diseases/PressureMassifDistribute', params: { tab_value: state.activeType } })
        const { data } = await get({ url: '/Diseases/PressureMassifDistribute', params: { tab_value: 1 } })
        commit('setCommonState', { key: 'farmlandDisease', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'farmlandDisease', value: false })
    }
  }
}
